package ysq.xyg.host.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.host.dao.HostItemDao;
import ysq.xyg.host.model.dto.HostItemDto;
import ysq.xyg.host.model.po.HostItem;
import ysq.xyg.host.model.vo.HostItemSearchVo;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.host.model.vo.HostItemVo;
import ysq.xyg.host.service.HostItemService;

import java.util.Date;
import java.util.List;

/**
 * 房产-楼盘service实现
 *
 * @author congcong tao
 * @date 2019-12-11 15:18:55
 */
@Service("hostItemService")
public class HostItemServiceImpl extends BaseServiceImpl<HostItem> implements HostItemService {

    @Autowired
    private HostItemDao hostItemDao;

    @Autowired
    public void setBaseDao(HostItemDao hostItemDao) {
        this.baseDao = hostItemDao;
    }

    /**
     * 分页查询
     *
     * @param hostItemVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 15:18:55
     */
    @Override
    public ReturnEntity page(HostItemVo hostItemVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<HostItem> list = acceptList(hostItemVo);
        PageInfo<HostItem> page = new PageInfo<HostItem>(list);
        return new ReturnEntity(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    @Override
    public ReturnEntity pageHostItemMobile(HostItemSearchVo hostItemSearchVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        List<HostItemDto> list = hostItemDao.selectEntitySearchList(hostItemSearchVo);
        PageInfo<HostItemDto> page = new PageInfo<>(list);
        return new ReturnEntity(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param hostItemVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 15:18:55
     */
    @Override
    public ReturnEntity addOrUpdate(HostItemVo hostItemVo, SysUser currentUser) {
        boolean isUpate = true;
        HostItem hostItem;
        if (StrUtil.isEmpty(hostItemVo.getId())) {
            hostItem = new HostItem(currentUser);
            isUpate = false;
        } else {
            hostItem = selectByPrimaryKey(hostItemVo.getId());
            if (ObjectUtil.isNull(hostItem)) {
                return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
            }
            hostItem.setUpdateTime(new Date());
            hostItem.setUpdateUser(currentUser.getId());
        }
        hostItem.setCityId(hostItemVo.getCityId());
        hostItem.setAreaId(hostItemVo.getAreaId());
        hostItem.setManageMemberId(hostItemVo.getManageMemberId());
        hostItem.setName(hostItemVo.getName());
        hostItem.setHostTag(hostItemVo.getHostTag());
        hostItem.setIsRecommend(hostItemVo.getIsRecommend());
        if (StringUtils.isNotEmpty(hostItemVo.getIsGrounding())) {
            hostItem.setIsGrounding(hostItemVo.getIsGrounding());
        } else {
            // 默认上架
            hostItem.setIsGrounding("2");
        }
        hostItem.setHostType(hostItemVo.getHostType());
        hostItem.setLayoutType(hostItemVo.getLayoutType());
        hostItem.setCoverImage(hostItemVo.getCoverImage());
        hostItem.setImages(hostItemVo.getImages());
        hostItem.setShareMoney(hostItemVo.getShareMoney());
        hostItem.setPrice(hostItemVo.getPrice());
        hostItem.setAllPrice(hostItemVo.getAllPrice());
        hostItem.setMinRooms(hostItemVo.getMinRooms());
        hostItem.setMaxRooms(hostItemVo.getMaxRooms());
        hostItem.setMinApartmentArea(hostItemVo.getMinApartmentArea());
        hostItem.setMaxApartmentArea(hostItemVo.getMaxApartmentArea());
        hostItem.setAddress(hostItemVo.getAddress());
        hostItem.setLatitude(hostItemVo.getLatitude());
        hostItem.setLongitude(hostItemVo.getLongitude());
        // 开盘时间
        hostItem.setOpenTime(DateUtil.parseDate(hostItemVo.getOpenTime()));
        // 交房时间
        hostItem.setDeliveryTime(DateUtil.parseDate(hostItemVo.getDeliveryTime()));
        hostItem.setPropertyYear(hostItemVo.getPropertyYear());
        hostItem.setHydroelectric(hostItemVo.getHydroelectric());
        hostItem.setDecorate(hostItemVo.getDecorate());
        hostItem.setBuilding(hostItemVo.getBuilding());
        hostItem.setOrderNum(hostItemVo.getOrderNum());
        int num;
        if (isUpate) {
            num = updateByPrimaryKey(hostItem);
        } else {
            num = insert(hostItem);
        }
        if (num <= 0) {
            return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 15:18:55
     */
    @Override
    public ReturnEntity delete(String id, SysUser currentUser) {
        HostItem hostItem = selectByPrimaryKey(id);
        if (hostItem != null) {
            hostItem.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            hostItem.setUpdateTime(new Date());
            hostItem.setUpdateUser(currentUser.getId());
            int num = updateByPrimaryKey(hostItem);
            if (num > 0) {
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param hostItemVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-12-11 15:18:55
     */
    @Override
    public List<HostItem> acceptList(HostItemVo hostItemVo){
        Example example = new Example(HostItem.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotEmpty(hostItemVo.getRemoveTag())) {
            criteria.andEqualTo("removeTag", hostItemVo.getRemoveTag());
        }
        if (StringUtil.isNotEmpty(hostItemVo.getAccountId())) {
            criteria.andEqualTo("accountId", hostItemVo.getAccountId());
        }
        if (StringUtil.isNotEmpty(hostItemVo.getName())) {
            criteria.andLike("name", "%" + hostItemVo.getName() + "%");
        }
        if (StringUtil.isNotEmpty(hostItemVo.getCityId())) {
            criteria.andEqualTo("cityId", hostItemVo.getCityId());
        }
        example.setOrderByClause(" update_time desc");
        return hostItemDao.selectByExample(example);
    }

    @Override
    public List<HostItemDto> selectHostItemList(String cityId, String isRecommend, int size) {
        return hostItemDao.selectHostItemList(cityId, isRecommend, size);
    }
}
